package com.itv.email.data;

import com.itv.email.bean.ADBean;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * User: bjxiajun
 * Date: 14-5-22
 * Time: 下午3:27
 * 读取预处理数据
 * 三级分类关系数据
 */
public class ReadPreprocessData {
    private static AtomicInteger urlIdCount=new AtomicInteger();
    public static Map<String,String> getCategoryMap(String filePath) throws IOException {
        filePath=filePath==null?"c3List.txt":filePath;
        File file=new File(filePath);
        InputStream is=new FileInputStream(file);
        InputStreamReader isr=new InputStreamReader(is);
        BufferedReader br=new BufferedReader(isr);
        String line;
        Map<String,String> map=new HashMap<String, String>(32);
        while((line=br.readLine())!=null){
            String [] c3=line.split(",");
            if(c3.length>=2){
                map.put(c3[0],c3[1]);
            }
        }
        is.close();
        isr.close();
        return map;
    }

    /**
     * 获取所有推广商品
     * @param filePath
     * @return
     * @throws IOException
     */
    public static Map<String,List<ADBean>> getADData(String filePath) throws IOException {
        filePath=filePath==null?"ad.txt":filePath;
        File file=new File(filePath);
        InputStream is=new FileInputStream(file);
        InputStreamReader isr=new InputStreamReader(is);
        BufferedReader br=new BufferedReader(isr);
        String line;
        Map<String,List<ADBean>> map=new HashMap<String,List<ADBean>>();
        while((line=br.readLine())!=null){
            String [] info=line.split(",");
            if(info.length>=6){
                ADBean ad=new ADBean();
                String c3=info[0].trim();
                ad.setC3(c3);
                ad.setTilte(info[1]);
                ad.setOldPric(info[2]);
                ad.setNewPric(info[3]);
                ad.setImg(info[4]);
                ad.setUrl(info[5]);
                int id=urlIdCount.getAndIncrement();
                ad.setUrlId(id);
                //System.out.println("insert into adEmail values("+id+",'"+ad.getUrl()+"','');");
                List<ADBean> li= map.get(c3);
                if(li==null){
                    li=new ArrayList<ADBean>();
                    map.put(c3,li);
                }
                li.add(ad);
            }
        }
        is.close();
        isr.close();
        return map;
    }

    public static void main(String[] args) throws IOException {
        ReadPreprocessData.getADData(null);
    }
}
